package com.linxianqin.springbootVue.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linxianqin.springbootVue.domain.dto.SysUserDTO;
import com.linxianqin.springbootVue.domain.entity.SysUserPost;
import com.linxianqin.springbootVue.mapper.SysUserPostMapper;
import com.linxianqin.springbootVue.service.SysUserPostService;
import com.linxianqin.springbootVue.utils.text.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户与岗位关联表 服务实现类
 * </p>
 *
 * @author lxq
 * @since 2020-12-18
 */
@Slf4j
@Service
public class SysUserPostServiceImpl extends ServiceImpl<SysUserPostMapper, SysUserPost> implements SysUserPostService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteUserPostByUserIds(List<Long> userIds) {
        baseMapper.deleteUserPostByUserIds(userIds);
    }
    /**
     * 新增用户岗位信息
     *
     * @param userDTO 用户对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertUserPost(SysUserDTO userDTO) {
        Long[] posts = userDTO.getPostIds();
        if (StringUtils.isNotNull(posts))
        {
            // 新增用户与岗位管理
            List<SysUserPost> list = new ArrayList<SysUserPost>();
            for (Long postId : posts)
            {
                SysUserPost up = new SysUserPost();
                up.setUserId(userDTO.getUserId());
                up.setPostId(postId);
                list.add(up);
            }
            if (list.size() > 0)
            {
                //保存所有
//                log.info("保存的岗位信息：{}",list);
                saveBatch(list,list.size());
            }
        }
    }
    /**
     * 删除用户与岗位关联
     * @param userId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteUserPostByUserId(Long userId) {
        LambdaQueryWrapper<SysUserPost> queryWrapper = new QueryWrapper<SysUserPost>().lambda();
        queryWrapper.eq(SysUserPost::getUserId,userId);
        baseMapper.delete(queryWrapper);
    }
}
